var app = getApp();

const qiniu = require("../../utils/qiniuUploader");

Page({

    /**
     * 页面的初始数据
     */
    data: {
        goods_id: "", // 商品id  编辑时
        auction_id: "", // 活动id

        name: '', // 商品标题
        intro: '', // 商品介绍
        price_status: "", // 出价方式
        price: '', // 起拍价格

        // array: ['1', '2', '3'],
        // index: 0,
        imgs: [], // 图片数据列表
        license: '',
        files_num: 0, // 图片id
        items: [
            {name: '竞价',value: '1',},
            {name: '一口价',value: '2'}
        ],
        item_index: 0, // 当前选中的出价模式

        status: true, // 是否未提交

        qiniu_token : "",     // 七牛token 
        qiniu_link : ""       // 七牛请求路径
    },
    inputGoodsData(e) {
        var value = e.detail.value;
        var type = e.target.dataset.type;
        if (type == "name") {
            this.setData({
                name: value
            })
        }
        if (type == "intro") {
            this.setData({
                intro: value
            })
        }
        if (type == "price") {
            this.setData({
                price: value
            })
        }
    },
    // 上传图片
    uploadImg(e) {
        var self = this;
        var imgs = this.data.imgs;
        var license = this.data.license;

        var qiniu_link = this.data.qiniu_link;
        var qiniu_token = this.data.qiniu_token;
        var uploadURL = "https://upload.qiniup.com";

        wx.chooseImage({
            count: 9,
            sizeType: ['original', 'compressed'],
            sourceType: ['album', 'camera'],
            success(res) {
                const tempFilePaths = res.tempFilePaths;
                for (var i = 0; i < tempFilePaths.length;i++){
                    var file = tempFilePaths[i];

                    var uploader = qiniu.upload(file, (res) => {
                        var url = res.imageURL;
                        var id = res.hash;
                        if (imgs.length >= 9) {
                            return false;
                        }
                        imgs.push({
                            id: id,
                            url: url
                        })
                        self.setData({
                            imgs: imgs,
                        })
                    },(error) => {
                        console.log('error: ' + error);
                    },{
                        uploadURL: uploadURL,
                        domain: qiniu_link,
                        uptoken: qiniu_token,
                    });
                } 
            }
        })
    },
    // 删除图片
    deleteImg(e) {
        var self = this;
        var type = e.target.dataset.type
        var id = e.target.dataset.id;
        var imgs = this.data.imgs;
        var license = this.data.license;
        console.log(imgs)
        imgs.map((item, index) => {
            if (item.id == id) {
                console.log(item.id, index)
                imgs.splice(index, 1)
            }
        })
        this.setData({
            imgs: imgs
        })
    },
    // 选择排序值
    bindPickerChange(e) {
        this.setData({
            index: e.detail.value
        })
    },
    // 选择 拍卖出价方式
    radioChange(e) {
        this.setData({
            price_status: e.detail.value
        })
    },
    verifyForm() {
        var self = this;
        var data = {};
        data.auction_id = this.data.auction_id;
        data.id = this.data.goods_id;
        data.type = 1;
        // data.sort = this.data.array[this.data.index];
        data.name = this.data.name;
        data.intro = this.data.intro;
        data.price_status = parseInt(this.data.price_status);
        data.start_price = this.data.price;
        var img = [];
        this.data.imgs.map(function(item, index) {
            img.push(item.url);
        })
        data.imgs = img.join(",");
        // if (!data.sort){
        // 	wx.showToast({
        // 		image: '../../img/warning.png',
        // 		title: '请选择拍品排序',
        // 	})
        // 	return false;
        // }
        if (!data.name) {
            wx.showToast({
                image: '../../img/warning.png',
                title: '请输入商品标题',
            })
            return false;
        }
        if (!data.intro) {
            wx.showToast({
                image: '../../img/warning.png',
                title: '请输入商品介绍',
            })
            return false;
        }
        if (!data.price_status) {
            wx.showToast({
                image: '../../img/warning.png',
                title: '请选择出价模式',
            })
            return false;
        }
        if (!data.start_price) {
            wx.showToast({
                image: '../../img/warning.png',
                title: '请输入起拍价',
            })
            return false;
        }
        if (!data.name) {
            wx.showToast({
                image: '../../img/warning.png',
                title: '请上传商品图片',
            })
            return false;
        }
        this.handleAucitonGoods(data);
    },
    // 添加 编辑商品
    handleAucitonGoods(formData) {
        var self = this;
        var url = "/auction/addAucitonGoods";
        var status = this.data.status;
        if (status) {
            self.setData({status:false})
            wx.getStorage({
                key: 'user',
                success: function(res) {

                    formData.openid = res.data.openid;
                    formData.token = res.data.token;

                    wx.request({
                        url: app.url.requestUrl + url,
                        method: 'POST',
                        data: formData,
                        success: function(res) {
                            var datas = res.data;
                            if (datas.code == 1) {
                                wx.showLoading({ title: '添加成功' })

                                setTimeout(function() {
                                    wx.hideLoading();
                                    wx.navigateBack({
                                        delta: 1
                                    })
                                }, 1000)
                            } else {
                                wx.showToast({ image: '../../img/warning.png',title: datas.msg })
                                self.setData({ status: true })
                            }
                        }
                    })
                }
            })
        }

    },
    // formid
    formSubmit: function(e) {
        console.log(e.detail.formId)
        var formId = e.detail.formId

        var that = this;
        wx.getStorage({
            key: 'user',
            success: function(res) {
                wx.request({
                    url: app.url.requestUrl + '/user/collect_formid',
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        formid: formId
                    },
                    success: function(res) {
                        if (res.data.code == 1) {


                        }

                    }
                })
            }
        })
    },
    // 获取七牛所需数据
    getQiniuInfo(){
        var self = this;
        var url = "/upload/token";

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token
                    },
                    success: function (res) {
                        var datas = res.data;
                        if (datas.code == 1) {
                            self.setData({
                                qiniu_token: datas.data.token,
                                qiniu_link: datas.data.url
                            })
                        }
                    }
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        var self = this;
        this.setData({
            price_status: this.data.items[0].value,
            goods_id: options.id ? options.id : '',
            auction_id: options.auction_id
        })
        this.getQiniuInfo();
        if (options.id) {
            this.getEditInfo(options.id)
        }
    },
    getEditInfo(id){
        var self = this;
        var url = "/auction/auction_goods_data";
        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token,
                        goods_id: id
                    },
                    success: function (res) {
                        var datas = res.data;
                        var imgs = [];
                        datas.data.imgs.map(function (item, index) {
                            imgs.push({
                                id: index,
                                url: item
                            })
                        })

                        if (datas.code == 1) {
                            self.setData({
                                name: datas.data.name,
                                intro: datas.data.intro,
                                imgs: imgs,
                                price: datas.data.start_price,
                                price_status: datas.data.price_status,
                            })
                        }
                    }
                })
            }
        })
    },
    //用户点击右上角分享
    onShareAppMessage: function() {

    }
})